package com.just.bz_back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.just.bz_back.dao.AdminMapper;
import com.just.bz_back.model.Admin;
import com.just.bz_back.model.AdminExample;
import com.just.bz_back.model.dto.Page;
import com.just.bz_back.service.AdminService;
import com.just.bz_back.util.JwtUtil;

import com.just.bz_back.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : baibaiwucahng
 * @date : 2019/8/31  11:57
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;
    @Override
    public String login(Admin admin) {
        AdminExample adminExample=new AdminExample();
        adminExample.createCriteria().andUsernameEqualTo(admin.getUsername());
        List<Admin> admins=adminMapper.selectByExample(adminExample);
        //用户不存在输出null
        if (admins==null)
        {
            return null;
        }
        String md5password= MD5Util.makeMd5(admin.getPassword());
        //如果密码错误输出error
        if (!md5password.equals(admins.get(0).getPassword()))
        {
            log.info("{}登陆失败",admin.getUsername());
            return "error";
        }
        //用户登陆成功输出token
        log.info("{}登陆成功",admin.getUsername());
        return JwtUtil.geneJsonWebToken(admins.get(0));
    }

    @Override
    public PageInfo<Admin> UserList(Page page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<Admin> list = adminMapper.selectByExample(null);
        PageInfo<Admin> adminPageInfo = new PageInfo<>(list);
        return adminPageInfo;
    }

    @Override
    public List<Admin> UserListAll() {
        return adminMapper.selectByExample(null);
    }
}
